

/*
 * @Descripttion: 
 * @version: 
 * @Author: 高月飞
 * @Date: 2023-11-15 19:54:32
 * @LastEditors: 高月飞
 * @LastEditTime: 2023-11-24 14:27:39
 */
import { userSystemPermissionApi } from "@/services/system"
import type { ICustomerData } from "@/types/customer"
import { errorMessage } from "@/utils/Tools/errorMessage"
import { ref, onMounted } from "vue"
import type { Ref } from 'vue'

export const userDialogVisible = (visible = false): [Ref<boolean>, (visible: boolean, callback?: () => void) => void] => {
    const addData = ref(false)
    const setData = (visible: boolean, callback: () => void = () => { }) => {
        addData.value = visible
        callback()
    }
    return [addData, setData]
}
//全部权限列表
export const usePermissionList = (
    pageNum: number = 1,
    query: object = {},
    initialSize: number = 10
): [Ref<ISystemData.IPermission[]>, Ref<number>, (page?: number, size?: number, query?: object) => Promise<void>] => {
    const data = ref<ISystemData.IPermission[]>([]);
    const total = ref(0);

    const loadData = async (page?: number, size?: number, query?: object) => {
        try {
            const ret = await userSystemPermissionApi(page, query, size);
            if (ret.code === 200) {
                data.value = ret.data.syspermission || [];
                total.value = ret.data.total || 0;
            } else {
                errorMessage('网络请求出现问题', () => {
                }, 3000)
            }
        } catch (error) {
            errorMessage('网络请求出现问题', () => {
            }, 3000)
        }
    };

    onMounted(() => {
        loadData(pageNum, initialSize, query).catch(error => {
            // 处理异常
            errorMessage('网络请求出现问题', () => {
            }, 3000)
        });
    });

    return [data, total, loadData];
};
